package com.zjj.lbw.designpattern.builder;

/**
 * @author zhanglei.zjj
 * @description 省略Director指挥者的建造者模式
 * @date 2023/7/12 17:35
 */
public class BuilderTestV2 {
    public static void main(String[] args) {
        ProductBuilder2 productBuilder2 = new DefaultProductBuilder2()
                .part1("part1")
                .part2("part2");

        // 模拟复杂业务情况
        if (true) {
            productBuilder2.part3("part3");
        }
        Product2 product2 = productBuilder2.part4("part4").build();
        System.out.println(product2);
    }
}

/**
 * 抽象建造者
 * 描述构建复杂对象需要创建那些部分以及把这些部分组装在一起的规范
 */
interface ProductBuilder2 {

    ProductBuilder2 part1(String part1);

    ProductBuilder2 part2(String part2);

    ProductBuilder2 part3(String part3);

    ProductBuilder2 part4(String part4);

    Product2 build();
}

/**
 * 具体建造者
 */
class DefaultProductBuilder2 implements ProductBuilder2 {

    private String part1;
    private String part2;
    private String part3;
    private String part4;

    @Override
    public ProductBuilder2 part1(String part1) {
        this.part1 = part1;
        return this;
    }

    @Override
    public ProductBuilder2 part2(String part2) {
        this.part2 = part2;
        return this;
    }

    @Override
    public ProductBuilder2 part3(String part3) {
        this.part3 = part3;
        return this;
    }

    @Override
    public ProductBuilder2 part4(String part4) {
        this.part4 = part4;
        return this;
    }

    @Override
    public Product2 build() {
        return new Product2(part1, part2, part3, part4);
    }
}

/**
 * 产品
 */
class Product2 {


    private String part1;
    private String part2;
    private String part3;
    private String part4;
    //  .....


    public Product2(String part1, String part2, String part3, String part4) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public String getPart3() {
        return part3;
    }

    public void setPart3(String part3) {
        this.part3 = part3;
    }

    public String getPart4() {
        return part4;
    }

    public void setPart4(String part4) {
        this.part4 = part4;
    }

    @Override
    public String toString() {
        return "Product2{" +
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                '}';
    }
}